package com.example.sql.manager.common;

import com.example.sql.manager.utils.MDCUtils;

import java.util.HashMap;

public class ResponseEntity<K, V> extends HashMap<K, V> {
    public static final String TRACE_ID_KEY = "traceId";
    private static final String DEFAULT_DATA_KEY = "data";
    private static final String DEFAULT_STATUS_KEY = "status";

    public static <K, V> ResponseEntity<K, V> build() {
        return new ResponseEntity<>();
    }

    public static ResponseEntity<String, Object> success(Object obj) {
        ResponseEntity<String, Object> response = build();
        response.set(DEFAULT_STATUS_KEY, 200);
        response.set(DEFAULT_DATA_KEY, obj);
        response.set(TRACE_ID_KEY, MDCUtils.get(TRACE_ID_KEY));
        return response;
    }

    public static ResponseEntity<String, Object> failure(Object obj) {
        ResponseEntity<String, Object> response = build();
        response.set(DEFAULT_STATUS_KEY, 500);
        response.set(DEFAULT_DATA_KEY, obj);
        response.set(TRACE_ID_KEY, MDCUtils.get(TRACE_ID_KEY));
        return response;
    }

    public void set(K key, V value) {
        if (key != null && value != null) this.putIfAbsent(key, value);
    }

}
